package cn.csc.module.product.controller.admin.seachcontext;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.seachcontext.vo.*;
import cn.csc.module.product.convert.seachcontext.SpuSeachContextConvert;
import cn.csc.module.product.dal.dataobject.seachcontext.SpuSeachContextDO;
import cn.csc.module.product.service.seachcontext.SpuSeachContextService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 商品搜索内容")
@RestController
@RequestMapping("/product/spu-seach-context")
@Validated
public class SpuSeachContextController {

    @Resource
    private SpuSeachContextService spuSeachContextService;

    @PostMapping("/create")
    @ApiOperation("创建商品搜索内容")
    @PreAuthorize("@ss.hasPermission('product:spu-seach-context:create')")
    public CommonResult<BigInteger> createSpuSeachContext(@Valid @RequestBody SpuSeachContextCreateReqVO createReqVO) {
        return success(spuSeachContextService.createSpuSeachContext(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品搜索内容")
    @PreAuthorize("@ss.hasPermission('product:spu-seach-context:update')")
    public CommonResult<Boolean> updateSpuSeachContext(@Valid @RequestBody SpuSeachContextUpdateReqVO updateReqVO) {
        spuSeachContextService.updateSpuSeachContext(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品搜索内容")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-seach-context:delete')")
    public CommonResult<Boolean> deleteSpuSeachContext(@RequestParam("id") BigInteger id) {
        spuSeachContextService.deleteSpuSeachContext(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品搜索内容")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-seach-context:query')")
    public CommonResult<SpuSeachContextRespVO> getSpuSeachContext(@RequestParam("id") BigInteger id) {
        SpuSeachContextDO spuSeachContext = spuSeachContextService.getSpuSeachContext(id);
        return success(SpuSeachContextConvert.INSTANCE.convert(spuSeachContext));
    }

    @GetMapping("/list")
    @ApiOperation("获得商品搜索内容列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spu-seach-context:query')")
    public CommonResult<List<SpuSeachContextRespVO>> getSpuSeachContextList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuSeachContextDO> list = spuSeachContextService.getSpuSeachContextList(ids);
        return success(SpuSeachContextConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品搜索内容分页")
    @PreAuthorize("@ss.hasPermission('product:spu-seach-context:query')")
    public CommonResult<PageResult<SpuSeachContextRespVO>> getSpuSeachContextPage(@Valid SpuSeachContextPageReqVO pageVO) {
        PageResult<SpuSeachContextDO> pageResult = spuSeachContextService.getSpuSeachContextPage(pageVO);
        return success(SpuSeachContextConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商品搜索内容 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-seach-context:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuSeachContextExcel(@Valid SpuSeachContextExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SpuSeachContextDO> list = spuSeachContextService.getSpuSeachContextList(exportReqVO);
        // 导出 Excel
        List<SpuSeachContextExcelVO> datas = SpuSeachContextConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品搜索内容.xls", "数据", SpuSeachContextExcelVO.class, datas);
    }

}
